# -*- coding:utf-8 -*-
# @author xupingmao <578749341@qq.com>
# @since 2020/08/22 21:54:56
# @modified 2022/03/19 10:20:23
import xauth
import xtemplate
import xutils
import sys
import platform
import xconfig
from xutils import dateutil
from xutils import fsutil
from xutils import mem_util

try:
    import sqlite3
except ImportError:
    sqlite3 = None

def get_xnote_version():
    return xconfig.get_global_config("system.version")

def get_mem_info():
    mem_used = 0
    result = mem_util.get_mem_info()

    mem_used = result.mem_used
    sys_mem_used = result.sys_mem_used
    sys_mem_total = result.sys_mem_total
    return "%s/%s/%s" % (mem_used, sys_mem_used, sys_mem_total)

def get_python_version():
    return sys.version

def get_startup_time():
    return dateutil.format_time(xconfig.START_TIME)

def get_free_data_space():
    try:
        size = fsutil.get_free_space(xconfig.get_system_dir("data"))
        return xutils.format_size(size)
    except:
        xutils.print_exc()
        return "<未知>"

class SystemInfoItem:

    def __init__(self, name = "", value = ""):
        self.name  = name
        self.value = value

class InfoHandler:

    @xauth.login_required("admin")
    def GET(self):
        p = xutils.get_argument("p", "")
        if p == "config_dict":
            return xconfig.get_config_dict()

        mem_info = mem_util.get_mem_info()
        sys_mem_info = "%s/%s" % (mem_info.sys_mem_used, mem_info.sys_mem_total)

        items = [
            SystemInfoItem("Python版本", value = get_python_version()),
            SystemInfoItem("Xnote版本", value = get_xnote_version()),
            SystemInfoItem("应用内存使用量", value = mem_info.mem_used),
            SystemInfoItem("系统内存使用量", value = sys_mem_info),
            SystemInfoItem("磁盘可用容量", get_free_data_space()),
            SystemInfoItem("数据库驱动", xconfig.get_system_config("db_driver")),
            SystemInfoItem("sqlite版本", sqlite3.sqlite_version if sqlite3 != None else ''),
            SystemInfoItem("CPU型号", platform.processor()),
            SystemInfoItem("操作系统", platform.system()),
            SystemInfoItem("操作系统版本", platform.version()),
            SystemInfoItem("系统启动时间", get_startup_time()),
        ]

        return xtemplate.render("system/page/system_info.html", items = items, 
            runtime_id = xconfig.RUNTIME_ID)


xurls = (
    r"/system/info", InfoHandler
)